package cn.appsys.service.devuser;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import org.springframework.stereotype.Service;

import cn.appsys.dao.appinfo.AppInfoMapper;
import cn.appsys.dao.appversion.VersionMapper;
import cn.appsys.pojo.AppInfo;

@Service
public class AppInfoServiceImpl implements AppInfoService {
	@Resource
	private AppInfoMapper appInfoMapper;
	@Resource
	private VersionMapper versionMapper;;

	/**
	 * 查询app列表
	 * 
	 * @author chenglong
	 * @param querySoftwareName
	 * @param queryStatus
	 * @param queryCategoryLevel1
	 * @param queryCategoryLevel2
	 * @param queryCategoryLevel3
	 * @param queryFlatformId
	 * @param devId
	 * @param currentPageNo
	 * @param pageSize
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<AppInfo> getAppInfoList(String querySoftwareName, Integer queryStatus, Integer queryCategoryLevel1,
			Integer queryCategoryLevel2, Integer queryCategoryLevel3, Integer queryFlatformId, Integer devId,
			Integer currentPageNo, Integer pageSize) throws Exception {
		return appInfoMapper.getAppInfoList(querySoftwareName, queryStatus, queryCategoryLevel1, queryCategoryLevel2,
				queryCategoryLevel3, queryFlatformId, devId, (currentPageNo - 1) * pageSize, pageSize);
	}

	/**
	 * 根据条件查询appInfo表记录数
	 * 
	 * @param querySoftwareName
	 * @param queryStatus
	 * @param queryCategoryLevel1
	 * @param queryCategoryLevel2
	 * @param queryCategoryLevel3
	 * @param queryFlatformId
	 * @param devId
	 * @return
	 * @throws Exception
	 */
	@Override
	public int getAppInfoCount(String querySoftwareName, Integer queryStatus, Integer queryCategoryLevel1,
			Integer queryCategoryLevel2, Integer queryCategoryLevel3, Integer queryFlatformId, Integer devId)
			throws Exception {
		return appInfoMapper.getAppInfoCount(querySoftwareName, queryStatus, queryCategoryLevel1, queryCategoryLevel2,
				queryCategoryLevel3, queryFlatformId, devId);
	}

	/**
	 * @author chenglong
	 * @param AppInfo
	 * @return
	 * @throws Exception
	 */
	@Override
	public boolean addAppInfo(AppInfo appinfo) throws Exception {
		return appInfoMapper.addAppInfo(appinfo);
	}

	@Override
	public int updateAppStatus(Integer id, Integer status, Integer modifyByid, Date date) {
		// TODO Auto-generated method stub
		return appInfoMapper.updateAppStatus(id, status, modifyByid, date);
	}

	/**
	 * 查看APKName是否存在
	 */
	@Override
	public boolean existAPK(String aPKName) {
		// TODO Auto-generated method stub
		if (appInfoMapper.existAPK(aPKName) == null)
			return true;
		return false;
	}

	@Override
	public AppInfo getAppInfobyId(Integer id) {
		// TODO Auto-generated method stub
		return appInfoMapper.getAppInfoById(id);
	}

	@Override
	public int updateAppInfo(AppInfo appInfo) {
		// TODO Auto-generated method stub
		return appInfoMapper.updateAppInfo(appInfo);
	}

	@Override
	public boolean updateAppLog(Integer id) {
		// TODO Auto-generated method stub
		return appInfoMapper.updateAppLog(id) > 0 ? true : false;
	}

	@Override
	public boolean updateAppStatus(Integer appinfoId, Integer status) {
		// TODO Auto-generated method stub
		// 修改app状态，
		if (appInfoMapper.updateStatus(appinfoId, status) > 0) {
			// 通过appid获取app对象
			AppInfo appInfo = appInfoMapper.getAppInfoById(appinfoId);
			int versionId = status == 5 ? 1 : 2;
			return versionMapper.updateVerPublishStatus(appInfo.getVersionId(), versionId) > 0 ? true : false;
		}
		// 修改版本的发布状态
		return false;
	}

	@Override
	public boolean delAppInfo(Integer appinfoId) {
		if (versionMapper.delVersion(appinfoId) > 0) {
			if (appInfoMapper.delAppInfo(appinfoId) > 0)
				return true;
		}
		return false;
	}
}
